package com.bleeth.fx.component;

import cn.hutool.core.util.StrUtil;
import com.bleeth.fx.font.MFontIcon;
import com.bleeth.fx.helper.StyleHelper;
import javafx.beans.property.SimpleStringProperty;
import javafx.geometry.Pos;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.paint.Paint;
import javafx.scene.shape.Circle;
import lombok.Builder;


/**
 * @author Sakura
 * @version 1.0
 * @date 2021-11-13 22:17
 **/
@Builder
public class MAvatar {

    private static final String TAG = "el-avatar";
    private static final String ICON_COLOR = "#ffffff";
    private static final double DEFAULT_SIZE = 50;
    private static final double DEFAULT_ICON_SIZE = 25;

    private String icon;

    /**
     * number / large / medium / small
     */
    private String size;

    /**
     * circle / square
     */
    @Builder.Default
    private String shape = "circle";


    private String color;

    /**
     * 图片头像的资源地址
     */
    private String src;
    private SimpleStringProperty _src;


    private String text;
    private SimpleStringProperty _text;


    public Region make() {
        HBox box = new HBox();
        box.setAlignment(Pos.CENTER);

        if (StrUtil.isEmpty(size)) {
            size = "large";
        }
        if (StrUtil.equalsIgnoreCase(size, "large")) {
            size = "50";
        }
        if (StrUtil.equalsIgnoreCase(size, "medium")) {
            size = "40";
        }
        if (StrUtil.equalsIgnoreCase(size, "small")) {
            size = "35";
        }
        Double dSize = Double.valueOf(size);
        box.setPrefSize(dSize, dSize);
        if (StrUtil.isNotEmpty(icon)) {
            box.getChildren().add(new MFontIcon(icon, dSize / 2, Color.web(ICON_COLOR)));
        }
        setStyle(box, dSize);
        box.setMaxHeight(dSize);
        return box;
    }

    private void setStyle(Region region, double size) {
        if (StrUtil.equalsIgnoreCase("circle", shape)) {
            region.setClip(new Circle(size / 2, size / 2, size / 2));
        }

        if (StrUtil.isNotEmpty(color)) {
            region.setBackground(new Background(
                    new BackgroundFill(Paint.valueOf(color), null, null)
            ));
        } else {
            region.setBackground(new Background(
                    new BackgroundFill(Paint.valueOf("#c0c4cc"), null, null)
            ));
        }

        region.hoverProperty().addListener((observable, oldValue, newValue) -> {
            StyleHelper.bg(region).color("#e2ffe2").make();
        });
    }

}
